package mipushdemo.xiaomi.com.diyitonghang.model.impl.zixunimpl;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import mipushdemo.xiaomi.com.diyitonghang.api.ApiService;
import mipushdemo.xiaomi.com.diyitonghang.app.AppUrl;
import mipushdemo.xiaomi.com.diyitonghang.data.bean.HomeDataBean;
import mipushdemo.xiaomi.com.diyitonghang.model.imodel.zixunmodel.IDataModel;
import mipushdemo.xiaomi.com.diyitonghang.utils.RetrofitUtils;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class DataModelImpl implements IDataModel {
    @Override
    public void responseData(String channelId, String userId, final SetOnDataChangedListener listener) {
        ApiService apiService = RetrofitUtils.newInstance().netWorkRequest();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("userId",userId);
            jsonObject.put("channelId",channelId);
            jsonObject.put("cursor","0");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = RequestBody.create(MediaType.parse(AppUrl.URL_HEADER), jsonObject.toString());
        Observable<HomeDataBean> observable = apiService.homeData(AppUrl.URL_HEADER, requestBody);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<HomeDataBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(HomeDataBean homeDataBean) {
                        ;listener.setOnData(homeDataBean);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
